import * as WorkflowApi from '@/api/business/flowable/workflow'
import { ref } from "vue";
import { message } from "ant-design-vue";

export function useWorkflowApi(loading) {
  const workflow = ref()
  const workflowList = ref([])

  async function findWorkflow(param) {
    try {
      loading && (loading.value = true)
      let temp = await WorkflowApi.findWorkflow(param)
      workflow.value = temp.data
    } catch (e) {
      console.error(e)
      message.error(e?.message || '查询失败')
    } finally {
      loading && (loading.value = false)
    }
  }

  async function findWorkflowList(param) {
    try {
      loading && (loading.value = true)
      let temp = await WorkflowApi.findWorkflowList(param)
      workflowList.value = temp.data
    } catch (e) {
      console.error(e)
      message.error(e?.message || '查询失败')
    } finally {
      loading && (loading.value = false)
    }
  }

  async function addWorkflow(data) {
    try {
      loading && (loading.value = true)
      await WorkflowApi.addWorkflow(data)
      message.success('新增成功')
    } catch (e) {
      console.error(e)
      message.error(e?.message || '新增失败')
    } finally {
      loading && (loading.value = false)
    }
  }

  async function updateWorkflow(data) {
    try {
      loading && (loading.value = true)
      await WorkflowApi.updateWorkflow(data)
      message.success('更新成功')
    } catch (e) {
      console.error(e)
      message.error(e?.message || '更新失败')
    } finally {
      loading && (loading.value = false)
    }
  }

  async function deleteWorkflow(id) {
    try {
      loading && (loading.value = true)
      await WorkflowApi.deleteWorkflow(id)
      message.success('删除成功')
    } catch (e) {
      console.error(e)
      message.error(e?.message || '删除失败')
    } finally {
      loading && (loading.value = false)
    }
  }

  async function deployWorkflow(id) {
    try {
      loading && (loading.value = true)
      await WorkflowApi.deployWorkflow(id)
      message.success('部署成功')
    } catch (e) {
      console.error(e)
      message.error(e?.message || '部署失败')
    } finally {
      loading && (loading.value = false)
    }
  }

  async function undeployWorkflow(id) {
    try {
      loading && (loading.value = true)
      await WorkflowApi.undeployWorkflow(id)
      message.success('取消部署成功')
    } catch (e) {
      console.error(e)
      message.error(e?.message || '取消部署失败')
    } finally {
      loading && (loading.value = false)
    }
  }

  async function suspendWorkflow(id) {
    try {
      loading && (loading.value = true)
      await WorkflowApi.suspendWorkflow(id)
      message.success('挂起成功')
    } catch (e) {
      console.error(e)
      message.error(e?.message || '挂起失败')
    } finally {
      loading && (loading.value = false)
    }
  }

  async function activateWorkflow(id) {
    try {
      loading && (loading.value = true)
      await WorkflowApi.activateWorkflow(id)
      message.success('激活成功')
    } catch (e) {
      console.error(e)
      message.error(e?.message || '激活失败')
    } finally {
      loading && (loading.value = false)
    }
  }

  return {
    workflow, findWorkflow,
    workflowList, findWorkflowList,
    addWorkflow, updateWorkflow, deleteWorkflow,
    deployWorkflow, undeployWorkflow,
    suspendWorkflow, activateWorkflow,
  }
} 